#define BLINKER_WIFI
#include <Blinker.h>
#define BLINKER_ESP_SMARTCONFIG
#include <ESP8266WiFi.h>         //包含ESP8266头文件，8266必须要使用的头文件
char auth[] = "9bfea613cd80";

char ssid[] = "%%%%%%%";
char pswd[] = "%%%%%%%%%%%%%";

BlinkerNumber HUMI("humi");           //定义湿度数据键名
BlinkerNumber TEMP("temp");           //定义温度数据键名
BlinkerNumber Number1("num-xinhao");  //定义信号强度键名
#define TEXTE_ip "tex-ip"
BlinkerText Text_ip(TEXTE_ip);

BlinkerNumber ALSCH1("ch1"); // Temperature
BlinkerNumber ALSCH0("ch0"); // Pressure
BlinkerNumber ALSSTATUS("ag"); // Altitude
BlinkerNumber ALSLUX("lux"); // Altitude
BlinkerNumber PSCH1("ps"); // Altitude
BlinkerNumber PSCH0("ir"); // Temperature
BlinkerNumber PSSTATUS("st"); // Pressure


BlinkerButton Btn_enable_ps("Enableps");
BlinkerButton Btn_enable_als("Enableals");
BlinkerButton Btn_int_ps("Intps");
BlinkerButton Btn_int_als("Intals");
BlinkerButton Btn_oled_ps("oledps");
BlinkerButton Btn_oled_als("oledals");
BlinkerButton Btn_thd_ps("thdps");
BlinkerButton Btn_liangdu_als("liangduals");
/**********************************************************/
void dataRead(const String & data)
{
  BLINKER_LOG("Blinker readString: ", data);

  Blinker.vibrate();

  uint32_t BlinkerTime = millis();

  Blinker.print("millis", BlinkerTime);
}
void btn_liangdu_als_callback(const String & state) {

  if (state == BLINKER_CMD_ON) {

    a4 = 1;
    b0 = 1;//打开als
    b1 = 1;
    Btn_liangdu_als.color("#00b0FF");
    Btn_liangdu_als.text("自动背光");
    Btn_liangdu_als.print("on");
    // Blinker.vibrate();
  }
  else if (state == BLINKER_CMD_OFF)  {

    a4 = 0;
    Btn_liangdu_als.color("#607E8B");
    Btn_liangdu_als.text("固定背光");
    Btn_liangdu_als.print("off");
    // Blinker.vibrate();
  }
}
void btn_enable_ps_callback(const String & state) {

  if (state == BLINKER_CMD_ON) {

    a0 = 1;
    a1 = 1;
    BLINKER_LOG("Toggle on!");
    Btn_enable_ps.color("#00b0FF");
    Btn_enable_ps.text("ps on");
    Btn_enable_ps.print("on");
    //Blinker.vibrate();//反馈震动
  }
  else if (state == BLINKER_CMD_OFF) {

    a0 = 0;
    a1 = 1;
    // Blinker.vibrate();
    BLINKER_LOG("Toggle off!");
    Btn_enable_ps.color("#607E8B");
    Btn_enable_ps.text("ps off");
    Btn_enable_ps.print("off");
  }

}
void btn_enable_als_callback(const String & state) {

  if (state == BLINKER_CMD_ON) {

    b0 = 1;
    b1 = 1;
    BLINKER_LOG("Toggle on!");
    Btn_enable_als.color("#00b0FF");
    Btn_enable_als.icon("far fa-lightbulb");
    Btn_enable_als.text("Als on");
    Btn_enable_als.print("on");
    // Blinker.vibrate();
  }
  else if (state == BLINKER_CMD_OFF)  {

    b0 = 0;
    b1 = 1;
    BLINKER_LOG("Toggle off!");
    Btn_enable_als.icon("far fa-lightbulb");
    Btn_enable_als.color("#607E8B");
    Btn_enable_als.text("Als off");
    Btn_enable_als.print("off");
    // Blinker.vibrate();
  }
}
void btn_int_ps_callback(const String & state) {

  if (state == BLINKER_CMD_ON) {
    a0 = 1;//打开ps
    b0 = 1;//打开als
    c0 = 1;
    c1 = 1;
    BLINKER_LOG("Toggle on!");
    Btn_int_ps.color("#00b0FF");
    Btn_int_ps.text("ps int on");
    Btn_int_ps.print("on");
    Btn_enable_als.print("on");
    Btn_enable_ps.print("on");
  }
  else if (state == BLINKER_CMD_OFF)  {
    a0 = 1;//打开ps
    b0 = 1;//打开als
    c0 = 0;
    c1 = 1;
    BLINKER_LOG("Toggle off!");
    Btn_int_ps.color("#607E8B");
    Btn_int_ps.text("ps int off");
    Btn_int_ps.print("off");
    Btn_enable_als.print("on");
    Btn_enable_ps.print("on");
  }
}
void btn_int_als_callback(const String & state) {

  if (state == BLINKER_CMD_ON) {
    a0 = 1;//打开ps
    b0 = 1;//打开als
    d0 = 1;
    d1 = 1;
    BLINKER_LOG("Toggle on!");
    Btn_int_als.color("#00b0FF");
    Btn_int_als.text("als int on");
    Btn_int_als.print("on");
    Btn_enable_als.print("on");
    Btn_enable_ps.print("on");
  }
  else if (state == BLINKER_CMD_OFF)  {
    a0 = 1;//打开ps
    b0 = 1;//打开als
    d0 = 0;
    d1 = 1;
    BLINKER_LOG("Toggle off!");
    Btn_int_als.color("#607E8B");
    Btn_int_als.text("als int off");
    Btn_int_als.print("off");
    Btn_enable_als.print("on");
    Btn_enable_ps.print("on");
  }
}

void btn_oled_ps_callback(const String & state) {

  if (state == BLINKER_CMD_ON) {

    a3 = 1;
    b3 = 0;
    c3 = 0;
    BLINKER_LOG("Toggle on!");
    Btn_oled_ps.color("#00b0FF");
    Btn_oled_ps.text("ps oled on");
    Btn_oled_ps.print("on");
    Btn_oled_als.print("off");
    Btn_thd_ps.print("off");
  }
  else if (state == BLINKER_CMD_OFF)  {

    a3 = 0;

    BLINKER_LOG("Toggle off!");
    Btn_oled_ps.color("#607E8B");
    Btn_oled_ps.text("ps oled off");
    Btn_oled_ps.print("off");
  }
}
void btn_oled_als_callback(const String & state) {

  if (state == BLINKER_CMD_ON) {

    b3 = 1;
    a3 = 0;
    c3 = 0;
    BLINKER_LOG("Toggle on!");
    Btn_oled_als.color("#00b0FF");
    Btn_oled_als.text("als oled on");
    Btn_oled_als.print("on");
    Btn_thd_ps.print("off");
    Btn_oled_ps.print("off");
  }
  else if (state == BLINKER_CMD_OFF)  {

    b3 = 0;
    BLINKER_LOG("Toggle off!");
    Btn_oled_als.color("#607E8B");
    Btn_oled_als.text("als oled off");
    Btn_oled_als.print("off");
  }
}
void btn_thd_ps_callback(const String & state) {
  if (state == BLINKER_CMD_ON) {
    c3 = 1;
    b3 = 0;
    a3 = 0;
    BLINKER_LOG("Toggle on!");
    Btn_thd_ps.color("#00b0FF");
    Btn_thd_ps.text("ps thd on");
    Btn_thd_ps.print("on");
    Btn_oled_als.print("off");
    Btn_oled_ps.print("off");
  }
  else if (state == BLINKER_CMD_OFF)  {
    c3 = 0;
    BLINKER_LOG("Toggle off!");
    Btn_thd_ps.color("#607E8B");
    Btn_thd_ps.text("ps thd off");
    Btn_thd_ps.print("off");
  }
}
void heartbeat()
{
  ALSCH0.print(mn_sensor_als_data_channel0);
  ALSCH1.print(mn_sensor_als_data_channel1);
  ALSSTATUS.print(mn_sensor_als_ag_current_stage);
  ALSLUX.print(mn_sensor_als_data_lux);
  PSCH0.print(mn_sensor_ps_data_ir_data);
  PSCH1.print(mn_sensor_ps_data_pdata);
  PSSTATUS.print(mTask_data_prox_data);
  huidiaoneirong();
}

/*********************************************************************************************************
  按键状态回调函数
*********************************************************************************************************/
void huidiaoneirong() {
  if (a0 == 1) {
    Btn_enable_ps.print("on");
  }
  else if (a0 == 0) {
    Btn_enable_ps.print("off");
  }
  if (b0 == 1) {
    Btn_enable_als.print("on");
  }
  else if (b0 == 0) {
    Btn_enable_als.print("off");
  }
  if (a3 == 0) {
    Btn_oled_ps.print("off");
  }
  if (b3 == 0) {
    Btn_oled_als.print("off");
  }
  if (c3 == 0) {
    Btn_thd_ps.print("off");
  }
  if (a3 == 1) {
    Btn_oled_ps.print("on");
  }
  else if (b3 == 1) {
    Btn_oled_als.print("on");
  }
  else if (c3 == 1) {
    Btn_thd_ps.print("on");
  }
  if (c0 == 1) {
    Btn_int_ps.print("on");
  }
  else if (c0 == 0) {
    Btn_int_ps.print("off");
  }
  if (d0 == 1) {
    Btn_int_als.print("on");
  }
  else if (d0 == 0) {
    Btn_int_als.print("off");
  }
  if (a4 == 1) {
    Btn_liangdu_als.print("on");
  }
  else if (a4 == 0) {
    Btn_liangdu_als.print("off");
  }

  Number1.icon("fal fa-wifi");
  Number1.print(WiFi.RSSI());
  Text_ip.print(WiFi.localIP().toString().c_str());

}
void initblinker()
{
  //WIFI_Set();


  Blinker.begin(auth, ssid, pswd);
  BLINKER_DEBUG.stream(Serial);
  //Blinker.begin(auth);

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);

  Blinker.attachData(dataRead);
  Blinker.attachHeartbeat(heartbeat);
  Btn_liangdu_als.attach(btn_liangdu_als_callback);
  Btn_enable_ps.attach(btn_enable_ps_callback);
  Btn_enable_als.attach(btn_enable_als_callback);
  Btn_int_ps.attach(btn_int_ps_callback);
  Btn_int_als.attach(btn_int_als_callback);
  Btn_oled_ps.attach(btn_oled_ps_callback);
  Btn_oled_als.attach(btn_oled_als_callback);
  Btn_thd_ps.attach(btn_thd_ps_callback);

}
void blinker()
{
  Blinker.run();
}
